//
//  DistinationViewController.m
//  HeartTravel
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DistinationViewController.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "DestinationCover.h"
#import "MyCell.h"
#import "PlaceViewController.h"
#import "MyCollectionReusableView.h"
#import "DetailViewController.h"
#import "DetailDestination.h"
#import "DistinationMoreViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface DistinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic, retain)NSMutableArray *titleArr;
@property(nonatomic, retain)NSMutableArray *playArr;
@property(nonatomic, retain)NSMutableArray *trendArr;
@property(nonatomic, retain)NSMutableArray *EuropeArr;
@property(nonatomic, retain)NSMutableArray *HMTArr;
@property(nonatomic, retain)NSMutableArray *AmericaArr;
@property(nonatomic, retain)NSMutableArray *AsiaArr;
@property(nonatomic, retain)NSMutableArray *domesticArr;
@property(nonatomic, retain)NSArray *sectionArr;
@property(nonatomic, retain)NSMutableArray *moreArr;


@end

@implementation DistinationViewController
- (void)dealloc{
    [_sectionArr release];
    [_titleArr release];
    [_playArr release];
    [_trendArr release];
    [_EuropeArr release];
    [_HMTArr release];
    [_AmericaArr release];
    [_AsiaArr release];
    [_domesticArr release];
    [_moreArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.sectionArr = [NSMutableArray array];
        self.titleArr = [NSMutableArray array];
        self.playArr = [NSMutableArray array];
        self.trendArr = [NSMutableArray array];
        self.EuropeArr = [NSMutableArray array];
        self.HMTArr = [NSMutableArray array];
        self.AmericaArr = [NSMutableArray array];
        self.AsiaArr = [NSMutableArray array];
        self.domesticArr = [NSMutableArray array];
        self.moreArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createView];
}

//解析数据
- (void)createData{
    
    [HTTPTool get:@"http://api.breadtrip.com/destination/v3/" body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        //        解析标题数据
        NSArray *elementsArr = dic[@"elements"];
        for (NSDictionary *tempDic in elementsArr) {
            [self.titleArr addObject:tempDic[@"title"]];
            NSArray *dataArr = tempDic[@"data"];
            dataArr = tempDic[@"data"];
            [self.moreArr addObject: tempDic[@"more"]];
            //            「本月推荐」玩起来
            if ([tempDic[@"title"] isEqualToString:@"「本月推荐」玩起来"]) {
                for (NSDictionary *temp2Dic in dataArr) {
                    DetailDestination *detail = [[DetailDestination alloc] init];
                    [detail setValuesForKeysWithDictionary:temp2Dic];
                    [self.playArr addObject:detail];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"热门地点"]) {
                //                热门地点
                for (NSDictionary *temp2Dic in dataArr) {
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.trendArr addObject:cover];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"欧洲国家"]) {
                for (NSDictionary *temp2Dic in dataArr) {
                    //                    欧洲国家
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.EuropeArr addObject:cover];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"港澳台"]) {
                //                港澳台
                for (NSDictionary *temp2Dic in dataArr) {
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.HMTArr addObject:cover];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"美洲和大洋洲"]) {
                //                美洲和大洋洲
                for (NSDictionary *temp2Dic in dataArr) {
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.AmericaArr addObject:cover];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"亚洲国家"]) {
                for (NSDictionary *temp2Dic in dataArr) {
                    //                    亚洲国家
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.AsiaArr addObject:cover];
                }
            }else if ([tempDic[@"title"] isEqualToString:@"国内城市"]) {
                for (NSDictionary *temp2Dic in dataArr) {
                    //                    国内城市
                    DestinationCover *cover = [[DestinationCover alloc] init];
                    [cover setValuesForKeysWithDictionary:temp2Dic];
                    [self.domesticArr addObject:cover];
                }
            }
        }
        self.sectionArr = @[self.playArr,self.trendArr,self.EuropeArr,self.HMTArr,self.AmericaArr,self.AsiaArr,self.domesticArr];
        UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1000];
        [collectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)createView{

    UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc] init];
    FlowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    FlowLayout.minimumLineSpacing = 10;
    FlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    FlowLayout.headerReferenceSize = CGSizeMake(0, 50);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64) collectionViewLayout:FlowLayout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView release];
    collectionView.backgroundColor = [UIColor whiteColor];
    
    [collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuseMyCell"];
    [collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
    
    collectionView.tag = 1000;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PlaceViewController *place = [[PlaceViewController alloc] init];
    DetailViewController *detail = [[DetailViewController alloc] init];
    if (indexPath.section == 0) {
        DetailDestination *detailDestination = self.playArr[indexPath.row];
       detail.bId = (NSNumber *)detailDestination.bId ;
        detail.type = detailDestination.type;
    }else if (indexPath.section == 1) {
        DestinationCover *cover = self.trendArr[indexPath.row];
       place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }else if (indexPath.section == 2) {
        DestinationCover *cover = self.EuropeArr[indexPath.row];
        place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }else if (indexPath.section == 3) {
        DestinationCover *cover = self.HMTArr[indexPath.row];
        place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }else if (indexPath.section == 4) {
        DestinationCover *cover = self.AmericaArr[indexPath.row];
        place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }else if (indexPath.section == 5) {
        DestinationCover *cover = self.AsiaArr[indexPath.row];
       place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }else if (indexPath.section == 6){
        DestinationCover *cover = self.domesticArr[indexPath.row];
        place.bId = (NSNumber *)cover.bId;
        place.type = cover.type;
    }
    if (indexPath.section == 0) {
        [self.navigationController pushViewController:detail animated:YES];
    }else if (indexPath.section > 0) {
        [self.navigationController pushViewController:place animated:YES];
    }
    [place release];
    [detail release];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView" forIndexPath:indexPath];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view.myLabel.text = [NSString stringWithFormat:@"%@",self.titleArr[indexPath.section]];
        view.isMore = NO;
        if (indexPath.section == 2) {
            view.isMore = YES;
            [view.moreButton addTarget:self action:@selector(more1:) forControlEvents:UIControlEventTouchUpInside];
        }else if(indexPath.section == 5){
            view.isMore = YES;
            [view.moreButton addTarget:self action:@selector(more2:) forControlEvents:UIControlEventTouchUpInside];
        }else if(indexPath.section == 6){
            view.isMore = YES;
            [view.moreButton addTarget:self action:@selector(more3:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            view.isMore = NO;
        }
    }
        return view;
}

- (void)more1:(UIButton *)button{
    DistinationMoreViewController *moreVC = [[DistinationMoreViewController alloc] init];
    [self.navigationController pushViewController:moreVC animated:YES];
    moreVC.index = 3;
    [moreVC release];
}

- (void)more2:(UIButton *)button{
    DistinationMoreViewController *moreVC = [[DistinationMoreViewController alloc] init];
    [self.navigationController pushViewController:moreVC animated:YES];
    moreVC.index = 6;
    [moreVC release];
}

- (void)more3:(UIButton *)button{
    DistinationMoreViewController *moreVC = [[DistinationMoreViewController alloc] init];
    [self.navigationController pushViewController:moreVC animated:YES];
    moreVC.index = 8;
    [moreVC release];
}

- (void)more:(UIButton *)button{

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.titleArr.count;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section == 0){
        return self.playArr.count;
    }else if(section == 1){
        return self.trendArr.count;
    }else if(section == 2){
        return self.EuropeArr.count;
    }else if(section == 3){
        return self.HMTArr.count;
    }else if(section == 4){
        return self.AmericaArr.count;
    }else if(section == 5){
        return self.AsiaArr.count;
    }else if(section == 6){
        return self.domesticArr.count;
    }else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseMyCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        DetailDestination *detail = self.playArr[indexPath.row];
        cell.myLabel.text = detail.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:detail.cover]];
        return cell;
    }else if (indexPath.section == 1) {
        DestinationCover *cover = self.trendArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else if (indexPath.section == 2) {
        DestinationCover *cover = self.EuropeArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else if (indexPath.section == 3) {
        DestinationCover *cover = self.HMTArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else if (indexPath.section == 4) {
        DestinationCover *cover = self.AmericaArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else if (indexPath.section == 5) {
        DestinationCover *cover = self.AsiaArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else if (indexPath.section == 6){
        DestinationCover *cover = self.domesticArr[indexPath.row];
        cell.myLabel.text = cover.name;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
        return cell;
    }else{
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
